package com.controller;

import com.pojo.Seckill;
import com.service.SeckillService;
import com.vo.Exposer;
import com.vo.SecKillResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by zc on 2017/6/10.
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<Seckill> seckillList = seckillService.getSeckillList();
        model.addAttribute("seckillList",seckillList);
        return "list";
    }

    //json 接口
    @RequestMapping(value = "/{seckillId}/exposer",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SecKillResult<Exposer> exposer(Long secKillId){
        SecKillResult<Exposer> result;
        try {
            Exposer exposer = seckillService.exportSecKillUrl(secKillId);
            result=new SecKillResult<Exposer>(true,exposer);
        } catch (Exception e) {
            result=new SecKillResult<Exposer>(false,e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

}
